package com.github.boluo1230.wechat.service.mp.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.boluo1230.wechat.constant.RedisConstant;
import com.github.boluo1230.wechat.entity.MpApp;
import com.github.boluo1230.wechat.entity.QrAutoReplyInfo;
import com.github.boluo1230.wechat.entity.WxUser;
import com.github.boluo1230.wechat.entity.vo.MpAutoReplyInfo;
import com.github.boluo1230.wechat.entity.vo.WxAccessToken;
import com.github.boluo1230.wechat.service.mp.RedisService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static com.github.boluo1230.wechat.constant.TimeConstant.ONE_MINUTE_SECONDS;

/**
 * @author Code Farmer
 * @date 2019/10/14 15:38
 */
@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public WxUser getWxUser(String appId, String openId) {
        String key = RedisConstant.KEY_USER_INFO_PREFIX + appId + "_" + openId;
        String value = redisTemplate.opsForValue().get(key);
        if (StringUtils.isBlank(value)) {
            return null;
        }
        return JSONObject.parseObject(value, WxUser.class);
    }

    @Override
    public void setWxUser(String appId, String openId, WxUser wxUser) {
        String key = RedisConstant.KEY_USER_INFO_PREFIX + appId + "_" + openId;
        redisTemplate.opsForValue().set(key, JSONObject.toJSONString(wxUser), ONE_MINUTE_SECONDS * 10, TimeUnit.SECONDS);
    }

    @Override
    public QrAutoReplyInfo getCachedMpAppQrAutoReplyConfig(String appId, String eventKey) {
        String key = RedisConstant.KEY_MP_APP_QR_AUTO_REPLY_PREFIX + appId + "_" + eventKey;
        String value = redisTemplate.opsForValue().get(key);
        if (StringUtils.isBlank(value)) {
            return null;
        }
        return JSONObject.parseObject(value, QrAutoReplyInfo.class);
    }

    @Override
    public void setCachedMpAppQrAutoReplyConfig(String appId, String eventKey, QrAutoReplyInfo qrAutoReplyInfo) {
        String key = RedisConstant.KEY_MP_APP_QR_AUTO_REPLY_PREFIX + appId + "_" + eventKey;
        redisTemplate.opsForValue().set(key, JSONObject.toJSONString(qrAutoReplyInfo), ONE_MINUTE_SECONDS * 10, TimeUnit.SECONDS);
    }

    @Override
    public MpAutoReplyInfo getCachedMpAppAutoReplyConfig(String appId) {
        String key = RedisConstant.KEY_MP_APP_AUTO_REPLY_PREFIX + appId;
        String value = redisTemplate.opsForValue().get(key);
        if (StringUtils.isBlank(value)) {
            return null;
        }
        return JSONObject.parseObject(value, MpAutoReplyInfo.class);
    }

    @Override
    public void cacheMpAppAutoReplyConfig(MpAutoReplyInfo mpAutoReplyInfo) {
        String key = RedisConstant.KEY_MP_APP_QR_AUTO_REPLY_PREFIX + mpAutoReplyInfo.getAppId();
        redisTemplate.opsForValue().set(key, JSONObject.toJSONString(mpAutoReplyInfo), ONE_MINUTE_SECONDS * 10, TimeUnit.SECONDS);
    }

    @Override
    public WxAccessToken getAccessToken(String appId) {
        String key = RedisConstant.KEY_MP_APP_ACCESS_TOKEN + appId;
        String value = redisTemplate.opsForValue().get(key);
        if (StringUtils.isBlank(value)) {
            return null;
        }
        return JSONObject.parseObject(value, WxAccessToken.class);
    }

    @Override
    public void setAccessToken(String appId, WxAccessToken wxAccessToken) {
        String key = RedisConstant.KEY_MP_APP_ACCESS_TOKEN + appId;
        redisTemplate.opsForValue().set(key, JSONObject.toJSONString(wxAccessToken), ONE_MINUTE_SECONDS * 30, TimeUnit.SECONDS);
    }

    @Override
    public List<MpApp> getAllMpApp() {
        String value = redisTemplate.opsForValue().get(RedisConstant.MP_APP_ALL);
        if (StringUtils.isBlank(value)) {
            return new ArrayList<>();
        }
        return JSONObject.parseArray(value, MpApp.class);
    }

    public void setAllMpApp(List<MpApp> mpAppList) {
        redisTemplate.opsForValue().set(RedisConstant.MP_APP_ALL, JSONObject.toJSONString(mpAppList), ONE_MINUTE_SECONDS * 30, TimeUnit.SECONDS);
    }

    @Override
    public boolean delAllMpApp() {
        return redisTemplate.delete(RedisConstant.MP_APP_ALL);
    }

    @Override
    public MpApp getCacheMpAppById(String appId) {
        String key = RedisConstant.MP_APP_ID + appId;
        String value = redisTemplate.opsForValue().get(key);
        if (StringUtils.isBlank(value)) {
            return null;
        }
        return JSONObject.parseObject(value, MpApp.class);
    }

    @Override
    public void setCacheMpAppById(String appId, MpApp mpApp) {
        String key = RedisConstant.MP_APP_ID + appId;
        redisTemplate.opsForValue().set(key, JSONObject.toJSONString(mpApp), ONE_MINUTE_SECONDS * 30, TimeUnit.SECONDS);
    }

    @Override
    public boolean delCacheMpAppById(String appId) {
        String key = RedisConstant.MP_APP_ID + appId;
        return redisTemplate.delete(key);
    }
}
